package com.king.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.king.entity.MUserMessage;
import com.king.service.MUserMessageService;
import com.king.service.WebSocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class WebSocketServiceImpl implements WebSocketService {

    @Autowired
    MUserMessageService messageService;

    @Autowired
    SimpMessagingTemplate messagingTemplate;

    @Async //异步执行
    @Override
    public void sendMsgCount(Long toUserId) {
        int count = messageService.count(new QueryWrapper<MUserMessage>()
                .eq("to_user_id", toUserId)
                .eq("status", "0")
                .orderByDesc("created")
        );

        //websocket通知（/user/6/msgCount）
        messagingTemplate.convertAndSendToUser(toUserId.toString(),"/msgCount",count);
    }
}
